package com.wk.springboot.controller;

import com.thunisoft.cloud.storage.client.base.protocol.attribtues.AttributeImpl;
import com.thunisoft.cloud.storage.client.base.protocol.attribtues.IAttributes;
import com.thunisoft.cloud.storage.client.core.exception.StorageException;
import com.thunisoft.cloud.storage.client.core.store.IStoreService;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Classname MinioController
 * @Description minio的Controller
 * @Date 2020/7/13 19:39
 * @Author by weikai
 */
@RestController
public class MinioController {

  @Resource
  private IStoreService storeService;

  @RequestMapping("getFile")
  public String getFile() throws IOException {
    InputStream inputStream = storeService
        .getInputStream("s3.sjzcgl-system:sjzcgl-jk/sjzcgl/test.doc");
    FileOutputStream fileOutputStream = new FileOutputStream("S:\\yl_test.txt");
    byte[] bytes = new byte[1024];
    int i = 0;
    while ((i=inputStream.read(bytes)) != -1){
      fileOutputStream.write(bytes);
    }
    return "ok";
  }

  @RequestMapping("upload")
  public void upload(MultipartFile multipartFile) throws StorageException {

/*    for(int i=0;i<10;i++){
      new Thread(()->{      //建立IAttributes
        String uuid = UUID.randomUUID().toString();
        IAttributes attributes = new AttributeImpl().setFileAscription("default")
            .setFilePrePath("fj").setFileName(uuid+"_536f3448a6314dbdadb4b2db7b9e8f73.doc")
            .setBucketName("sjzcgl");
        try {
          storeService.store(multipartFile.getInputStream(), attributes);
        } catch (IOException e) {
          e.printStackTrace();
        }
      }).start();*/

      String uuid = UUID.randomUUID().toString();
      IAttributes attributes = new AttributeImpl().setFileAscription("default")
          .setFilePrePath("fj").setFileName(uuid+"_536f3448a6314dbdadb4b2db7b9e8f73.doc")
          .setBucketName("sjzcgl");
      try {
        storeService.store(multipartFile.getInputStream(), attributes);
      } catch (IOException e) {
        e.printStackTrace();
      }

  }

}
